<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('BankHelper');
$BankHelper =  new BankHelper();

$this->LoadHelper('SettingHelper');
$SettingHelper = new SettingHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$this->LoadHelper('BranchHelper');
$BranchHelper = new MessageHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();

$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL.'/OrderDetails/index/?code='.$dp_code.'&sign='.$sign['sign'];
$model = $this->LoadModel('Public');
if (empty($this->Param['option'])) {

	//待审核列表
	$ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    if ($ajax) {
        $where = array(
            '`o_status` IN(?)' => array('初审提交', '审核中'),
        );
        //$base_url = \yii2api\Config::$base_url;
        //$uri = 'chedai/online-verify/check-has-result';
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info, 'order', array('o_suspend_status DESC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        $mode = Config::GetCommon();
        if (!empty($data['all'])){
        	foreach($data['all'] AS $rowNum => $row){
               //$vip_cert = $model->getOneRow('order_cert','*',array('`ct_oid` =?' =>$row['o_id'],'`ct_key` =?' =>'vip'));
               //$body = array(
               //    "ct_id" => $vip_cert['ct_id']
               //);
               //$reponse = \Httpful\Request::post($base_url.$uri)->body($body)->sendsType(\Httpful\Mime::FORM)->send();
               //if($reponse->body->code == 200){
               //    $row['video_audit'] = 1;
               //}else{
               //    if($reponse->body->message == '远程面签正在审核，请等待'){
               //        $row['video_audit'] = 1;
               //    }
               //}

               $row['o_sell_time'] = date('Y-m-d H:i:s', $row['o_sell_time']);
               $row['o_search_time'] = date('Y-m-d H:i:s', $row['o_search_time']);
               $row['o_first_time'] = date('Y-m-d H:i:s', $row['o_first_time']); //初审提交时间
               $order_car = $OrderHelper->orderCar($row['o_id']);
               $row['ot_loan_limit'] = $order_car['ot_loan_limit']; //贷款金额
               $row['o_pre_modetext'] = $mode['mode'][$row['o_pre_mode']]['mode_text'];
               $o_perch = $MemberHelper->getMemberRow($row['o_final_perch']);
               $row['o_final_perch_name'] = $o_perch['u_realname']; //终审占位人员

               $o_video_perch = $MemberHelper->getMemberRow($row['o_videoaudit_perch']);
               $row['o_videoaudit_perch_name'] = $o_video_perch['u_realname'];
               $bank_row = $BankHelper->bankRow($row['o_bank_id']);
               $row['o_bank_abb'] = $bank_row['bk_abbreviation'];

                if ($row['o_oldsys'] == 1) {
                    $sign = $oldapi->sign(array('code' => $dp_code));
                    $row['info_url'] = $api_baseurl . '&id=' . $row['o_number'];
                } else {
                    $row['info_url'] = '/index/index/option/info?oid=' . $row['o_id'];
                }
                $row['o_suspend_remarks'] = '';
                if ($row['o_layer_status'] == '暂停中') {
                    $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fao');
                    if(!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks']."电审【".$row['o_final_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                }
                if ($row['o_trial_status'] == '暂停中') {
                    $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fto');
                    if(!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks']."资审【".$row['o_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                }
                if ($row['o_videoaudit_status'] == '暂停中') {
                    $audit_msg = $OrderHelper->getOneAudit($row['o_id'], 'fvo');
                    if(!empty($audit_msg)) $row['o_suspend_remarks'] = $row['o_suspend_remarks']."视频审【".$row['o_videoaudit_perch_name'] . "】于" . $audit_msg['a_dateline'] . "暂停审核,原因为:\n" . $audit_msg['a_remarks'] . "\n";
                }
                if(!empty($row['o_suspend_remarks']))
                    $row['o_suspend_remarks'] = $row['o_suspend_remarks']."如需继续审核请及时使用“订单申请修改”功能！";
                $json_data['rows'][] = $row;
            }
        }
            echo json_encode($json_data);
    } else {
        echo $this->GetView('order_dshlist.php');
    }
}else {
    switch ($this->Param['option']) {
      case 'dshremove':
          $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
          $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
          if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
          $result = $OrderHelper->orderInvalid($oid);
          if($result['success']){
            $MemberHelper->saveLog($this->UserConfig, '作废客户', json_encode($this->Param), $this->Controller.'_'.$this->Action,$oid);
            ErrorMsg::Debug('处理成功', TRUE);
          }else{
           ErrorMsg::Debug($result['msg']);
            }
          break;
      default :
    }
}
